
-- 1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select s.s_id '编号',s.s_name '姓名',s.s_age '出生日期',s.s_sex '性别',ss.aa '语文',ss.bb '数学'
from (select a.s_id,a.score as aa,b.score as bb from (select * from my_schema.sc where sc.c_id=1) as a
inner join (select * from my_schema.sc where sc.c_id=2) as b on a.s_id =b.s_id
where a.score>b.score) ss
left join my_schema.student s
on s.s_id=ss.s_id;

-- 1.1 查询同时存在" 01 "课程和" 02 "课程的情况
select *
from  my_schema.student s
inner join (select sc1.a c1,sc1.ch1,sc2.ch2
from (select s_id b,score ch2 from my_schema.sc where sc.c_id=2) as sc2
inner join(select s_id a,score ch1 from my_schema.sc where sc.c_id=1) as sc1
on sc1.a=sc2.b) as aa
on aa.c1=s.s_id;

select sc1.a ,sc1.ch1 '语文',sc2.ch2 '数学'
from (select s_id b,score ch2 from my_schema.sc where sc.c_id=2) as sc2
inner join(select s_id a,score ch1 from my_schema.sc where sc.c_id=1) as sc1
on sc1.a=sc2.b;

-- 1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select sc1.s_id '学生编号',sc1.c_id '语文',sc1.score '成绩',sc2.c_id '数学',sc2.score '成绩'
from (select * from my_schema.sc where sc.c_id=1) as sc1
left join(select *from my_schema.sc where sc.c_id=2) as sc2
on sc1.s_id=sc2.s_id;

-- 1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from my_schema.sc
where sc.c_id = '02' and sc.s_id not in (select sc.s_id from my_schema.sc where sc.c_id = '01');

-- 2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select s.s_id,s.s_name'姓名',a.ag '平均成绩'
from my_schema.student s
inner join (
    select s_id,avg(score) ag
    from my_schema.sc
    group by s_id having ag>=60
) a
on s.s_id=a.s_id;


-- 3. 查询在 SC 表存在成绩的学生信息
select s.*
from my_schema.student s
where s.s_id in (select sc.s_id from my_schema.sc where sc.c_id >= 0);

select distinct stu.* from my_schema.student as stu join my_schema.sc on stu.s_id = sc.s_id;

-- 4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null )
select *
from my_schema.student s
left join (select count(1) '选课总数',sum(sc.score) '总成绩',s_id id from my_schema.sc group by sc.s_id) as ss
on ss.id=s.s_id;

-- 4.1 查有成绩的学生信息
select distinct s.* from my_schema.student as s join my_schema.sc on sc.s_id=s.s_id;

-- 5. 查询「李」姓老师的数量
select count(1)
from (select t.t_name from my_schema.teacher t where t.t_name like '李%') as t;

select count(1)
from my_schema.teacher t
where t.t_name like '李%';

-- 6. 查询学过「张三」老师授课的同学的信息
select *
from student s
where s.s_id in (select sc.s_id from sc 
where sc.c_id = (select c.c_id from course c 
where c.t_id=(select t.t_id from teacher t where t.t_name='张三')));


-- 7. 查询没有学全所有课程的同学的信息
select *
from my_schema.student s
left join (select sc.s_id id from my_schema.sc group by sc.s_id having count(1)<(select count(1) from course)) as sc
on sc.id=s.s_id
where sc.id=s.s_id or s.s_id not in (select sc.s_id from sc);

select * from student where s_id in (
select s_id from sc group by s_id having count(c_id) <
(select count(1) from course)
);

-- 8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct s.*
from student s
inner join sc
on sc.s_id=s.s_id
where sc.c_id in (select sc.c_id from sc where sc.s_id=1);

-- 9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select s2.s_id,student.s_name
from sc as s1
join sc as s2
on s1.c_id = s2.c_id and s1.s_id = '01' and s2.s_id != '01'
join student on s2.s_id = student.s_id
group by s2.s_id,student.s_name
having count(s2.c_id) = (select count(*) from sc where s_id = '01');

select c_id from sc where s_id='01';
select * from sc where c_id in (
select c_id from sc where s_id='01'
) and s_id not in(
select s_id from sc where c_id not in (
select c_id from sc where s_id='01'
))
group by s_id
having count(c_id)=(select count(c_id) from sc where s_id='01')
;


-- 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select s.s_name '姓名'
from student s
where s.s_id not in (select sc.s_id from sc 
where sc.c_id in (select c.c_id from course c 
where c.t_id in(select t.t_id from teacher t where t.t_name='张三')));

-- 11. 查询两门及其以上不及格课程的同学的学号，姓名及其平均成绩
select stu.s_id,stu.s_name,round(avg(sc.score),2) as avg_score
from student as stu
join sc on stu.s_id = sc.s_id
where sc.score < 60
group by stu.s_id,stu.s_name having count(sc.c_id) >= 2;

select *
from student s
right join(
select s_id,avg(score) avgs from sc where score <60
group by s_id having count(score) >= 2
) a
on s.s_id=a.s_id;

-- 12. 检索" 01 "课程分数小于 60，按分数降序排列的学生信息
select s.*,ss.sc
from student s
inner join(select sc.s_id a,sc.score sc from sc where sc.score<60 and sc.c_id =01) as ss
on s.s_id=ss.a
order by ss.sc desc;

-- 18. 查询各科成绩前三名的记录
select s.*,sc1.*
from student s, sc sc1
where (select count(1) from sc sc2 where sc1.c_id = sc2.c_id and sc1.score < sc2.score) < 3 and s.s_id=sc1.s_id
order by sc1.c_id, sc1.score desc;

select * from sc sc1
where (select count(1) from sc where c_id=sc1.c_id and score >sc1.score)<3 order by sc1.c_id,sc1.score desc;

-- 19. 查询每门课程被选修的学生数
select *
from course c
inner join (select sc.c_id id, count(1) from sc group by sc.c_id) as sc
on sc.id = c.c_id;

-- 20. 查询出只选修两门课程的学生学号和姓名
select s.s_id,s.s_name
from student s
inner join (select sc.s_id id from sc  group by sc.s_id having count(1) =2) as sc
on sc.id=s.s_id;

-- 21. 查询男生、女生人数
select s.s_sex,count(1)
from student s
group by s.s_sex;

-- 22. 查询名字中含有「风」字的学生信息
select *
from student s
where s.s_name like '%风%';
-- 23. 查询同名同性学生名单，并统计同名人数
select s.*,count(1) c from student s group by s.s_name ;

select s1.*,s2.c
from student s1
inner join (select s.*,count(1)-1 c from student s group by s.s_name) as s2
on s1.s_name=s2.s_name
where s2.c > 0;

select s.*,a.cc
from student s
left join(select s_name,s_sex,count(1) cc from student group by s_name,s_sex) a
on s.s_name=a.s_name and s.s_sex=a.s_sex
where a.cc > 1;

-- 24. 查询 1990 年出生的学生名单
select *
from student s
where s.s_age like '1990%';

select * from student s where year(s_age)=1990;

-- 25. 查询每门课程的平均成绩，结果按平均成绩降序排列，平均成绩相同时，按课程编号升序排列
select distinct  sc2.*
from sc sc1
inner join(select sc.c_id id,avg(sc.score) sco from sc group by sc.c_id) as sc2
order by sc1.c_id,sc2.sco desc;

-- 26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select s.s_id,s.s_name,sc.ag
from student s
inner join(select avg(sc.score) ag,sc.s_id id from sc group by sc.s_id) as sc
on sc.id=s.s_id
where sc.ag>=85;

-- 27. 查询课程名称为「数学」，且分数低于 60 的学生姓名和分数
select s.s_name,sc.sco
from student s
inner join(select sc.score sco,sc.s_id id from sc where sc.c_id = (select c.c_id from course c where c.c_name='数学') and sc.score <60) as sc
where s.s_id=sc.id;

-- 28. 查询所有学生的课程及分数情况（存在学生没成绩，没选课的情况）
select *
from student s
left join sc
on s.s_id=sc.s_id
left join course c
on c.c_id=sc.c_id
order by s.s_id;

-- 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select *
from sc,course c
where sc.c_id=c.c_id and sc.score >70;

select s.s_name,c.c_name,sc.score
from student s
inner join sc on sc.s_id=s.s_id
inner join course c on c.c_id=sc.c_id
where sc.score>70;

-- 30. 查询不及格的课程
select *
from sc,course c
where sc.c_id=c.c_id and sc.score<60;

select s.s_name,c.c_name,sc.score
from student s
inner join sc on sc.s_id=s.s_id
inner join course c on c.c_id=sc.c_id
where sc.score<60 order by c.c_id;

-- 31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名(不包括80分没有，包括80分有两个)
select s.s_id,s.s_name
from student s
where s.s_id in (select sc.s_id from sc where sc.c_id=1 and sc.score>=80);

-- 32. 求每门课程的学生人数
select distinct c.c_name,sc.cou
from course c,(select count(1) cou from sc group by sc.c_id) as sc;

-- 33. 成绩不重复，查询选修「张三」老师所授课程的学生中，成绩最高的学生信息及其成绩
select *
from course c,teacher t
where c.t_id=t.t_id and t.t_name='张三';

select *
from sc
inner join course c
on sc.c_id=c.c_id
inner join teacher t
on c.t_id=t.t_id
where t.t_name='张三';

select *
from student s
inner join (select max(sc.score),sc.* from sc group by sc.c_id) as sc on s.s_id=sc.s_id
where sc.c_id=(select c.c_id from course c where c.t_id = (select t.t_id from teacher t where t.t_name='张三'));



-- 34. 成绩有重复的情况下，查询选修「张三」老师所授课程的学生中，成绩最高的学生信息及其成绩
select s.*,sc1.*
from student s,sc sc1
where sc1.c_id=(select c.c_id from course c where c.t_id = (select t.t_id from teacher t where t.t_name='张三')) 
and (select count(1) from sc sc2 where sc2.c_id=sc1.c_id and sc1.score<sc2.score)<1
and sc1.s_id=s.s_id
order by sc1.score desc;
-- 37. 统计每门课程的学生选修人数（超过 5 人的课程才统计）。
select c.c_name,sc.cou
from (select count(1) cou,sc.* from sc group by sc.c_id) as sc,course c
where c.c_id=sc.c_id and sc.cou>5
order by sc.cou desc,c.c_id;

-- 38. 检索至少选修两门课程的学生学号
select s.s_id,s.s_name,sc.cou
from(select count(1) cou,sc.* from sc group by sc.s_id) as sc,student s
where s.s_id=sc.s_id and sc.cou>=2;

-- 39. 查询选修了全部课程的学生信息
select s.*,sc.cou
from student s,(select count(1) cou,sc.* from sc group by sc.s_id) as sc
where sc.s_id=s.s_id and sc.cou=(select count(1) from course);